package com.aaa.sbm.util;

import com.aaa.sbm.entity.User;
import com.aaa.sbm.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal =(String) authenticationToken.getPrincipal();
        Map userMap=new HashMap();
        userMap.put("userName",principal);
        List<User> list = userService.list(userMap);
        if(list==null || list.size()==0){
            throw new AccountException();
        }
        User user = list.get(0);
        String passWord = user.getPassWord();
        String passWordSalt = user.getPassWordSalt();
        return new SimpleAuthenticationInfo(user,passWord, ByteSource.Util.bytes(passWordSalt),getName());
    }
}
